package com.guoguo.sincemusic.ui

import android.view.Gravity
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.guoguo.sincemusic.R
import com.guoguo.sincemusic.databinding.ActivityMainBinding
import com.guoguo.sincemusic.ui.common.Adapter.ViewPageFragmentAdapter
import com.guoguo.sincemusic.ui.common.BaseActivity
import com.guoguo.sincemusic.ui.main.FavoritesFragment
import com.guoguo.sincemusic.ui.main.ProfileFragment
import com.guoguo.sincemusic.utils.Tools

class MainActivity : BaseActivity() {
    companion object {
        var mainActivity: MainActivity? = null
    }

    private lateinit var adapter: ViewPageFragmentAdapter
    private lateinit var viewPage: ViewPager
    private lateinit var binding: ActivityMainBinding
    private var fragments: MutableList<Fragment> = ArrayList()

    override fun initBinding() {
        super.initBinding()
        mainActivity = this
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    override fun initBar() {
        super.initBar()
        Tools.setSystemBarColor(this@MainActivity, R.color.grey_5)
        Tools.setSystemBarLight(this)
    }

    //设置 Fragment适配器
    override fun initView() {
        super.initView()
        viewPage = binding.viewPager
        fragments.apply {
            add(FavoritesFragment())
            add(ProfileFragment())
        }
        adapter = ViewPageFragmentAdapter(fragments, supportFragmentManager)
        viewPage.adapter = adapter
    }

    override fun initListener() {
        super.initListener()
        //viewpage页面切换
        viewPage.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }
            // 页面滑动切换的时候改状态栏颜色
            override fun onPageSelected(position: Int) {
                binding.navigation.menu.getItem(position).isChecked = true
                if (position == 0)  Tools.setSystemBarColor(this@MainActivity, R.color.grey_5)
                if (position == 1)  Tools.setSystemBarColor(this@MainActivity, R.color.blue_500)
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
        // 导航栏点击切换 并改状态栏颜色
        binding.navigation.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.navigation_favorites -> {
                    Tools.setSystemBarColor(this, R.color.grey_5)
                    viewPage.currentItem = 0
                }
                R.id.navigation_nearby ->{
                    viewPage.currentItem = 1
                    Tools.setSystemBarColor(this, R.color.blue_500)
                }
            }
            false
        }
    }

    // 提供给fragment 调用的方法
    fun toNavigation(hide: Boolean) {
        animateNavigation(hide)
    }
    // 从frament 中打开Drawer
    fun openDrawer(){
        with(binding) {
            drawerLayout.openDrawer(Gravity.LEFT)
        }
    }

    // 底部导航栏隐藏
    var isNavigationHide = false
    private fun animateNavigation(hide: Boolean) {
        val navigation = binding.navigation
        if (isNavigationHide && hide || !isNavigationHide && !hide) return
        isNavigationHide = hide
        val moveY = if (hide) 2 * navigation.height else 0
        navigation.animate().translationY(moveY.toFloat()).setStartDelay(100).setDuration(300)
            .start()
    }
}